package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblRrAccountSubject;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblRrAccountSubjectService;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * RR-会计科目 前端控制器
 * @author xiaochuan
 * @since 2023-10-17
 */

@Slf4j
@Api(tags = "RR-会计科目",description = "RR-会计科目")
@RestController
@RequestMapping("/rr/account_subject")
public class TblRrAccountSubjectController extends ExcelController<TblRrAccountSubject> {

    @Resource
    private ITblRrAccountSubjectService tblRrAccountSubjectService;

    @Override
    public Class<?> getModelClass() {
        return TblRrAccountSubject.class;
    }

    @Override
    public IBaseService init() {
        return tblRrAccountSubjectService;
    }

    @Override
    public String verifyImportObject(TblRrAccountSubject tblRrAccountSubject, AnalysisContext analysisContext) {

        String s = super.verifyImportObject(tblRrAccountSubject, analysisContext);
        if(StringUtils.isNotEmpty(s)){
            return s;
        }

        try {
            TblRrAccountSubject subject = tblRrAccountSubjectService.selectExistSubject(tblRrAccountSubject);
            if(subject!=null){
                log.info("数据已存在");
                return "数据已存在";
            }
        } catch (ServiceException e) {
            log.error(e.getMessage(),e);
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    protected String validateAllDate(List<TblRrAccountSubject> datas) {
        List<String> list = new ArrayList<>();
        for (TblRrAccountSubject data : datas) {
            String toString = data.toUniqueString();
            if (list.contains(toString)) {
                log.info("有两行数据重复");
                return "有两行数据重复";
            } else {
                list.add(toString);
            }
        }

        return super.validateAllDate(datas);
    }
}

